package com.dms.modules.user.entity;

import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;

@Data
@TableName("user")
@ApiModel(value = "User对象", description = "用户")
public class User implements UserDetails {
    
    @TableId(type = IdType.AUTO)
    @ApiModelProperty("用户ID")
    private Long id;
    
    @ApiModelProperty("用户名")
    private String username;
    
    @ApiModelProperty("密码")
    private String password;
    
    @ApiModelProperty("昵称")
    private String nickname;
    
    @ApiModelProperty("头像")
    private String avatar;
    
    @ApiModelProperty("性别 0-未知 1-男 2-女")
    private Integer gender;
    
    @ApiModelProperty("手机号")
    private String phone;
    
    @ApiModelProperty("邮箱")
    private String email;
    
    @ApiModelProperty("生日")
    private String birthday;
    
    @ApiModelProperty("个性签名")
    private String signature;
    
    @ApiModelProperty("角色ID：1-管理员 2-商家 3-用户")
    private Long roleId;
    
    @ApiModelProperty("状态：0-禁用 1-正常")
    private Integer status;
    
    @ApiModelProperty("创建时间")
    private Date createTime;
    
    @ApiModelProperty("更新时间")
    private Date updateTime;

    @ApiModelProperty("最后登录时间")
    private Date lastLoginTime;
    
    @TableLogic
    @ApiModelProperty("是否删除")
    private Integer deleted;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String roleCode = "ROLE_USER";
        if (this.roleId != null) {
            switch (this.roleId.intValue()) {
                case 1:
                    roleCode = "ROLE_ADMIN";
                    break;
                case 2:
                    roleCode = "ROLE_MERCHANT";
                    break;
                case 3:
                    roleCode = "ROLE_USER";
                    break;
                default:
                    roleCode = "ROLE_USER";
            }
        }
        return Collections.singletonList(new SimpleGrantedAuthority(roleCode));
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
} 